import axios, {AxiosError, AxiosResponse} from "axios";
import {Temp} from "./Constant";

export const TargetServer = "http://192.168.20.4:8080"


// 创建实例时配置默认值
export const Instance = axios.create({
    timeout: 2500,
    baseURL: TargetServer,
});


// 添加请求拦截器
Instance.interceptors.request.use(function (config) {
    const token = localStorage.getItem(Temp.Token)
    if (config && config.headers) {
        config.headers[Temp.Token] = token
    }
    return config;
}, function (error) {
    return Promise.reject(error);
});
// 添加响应拦截器
Instance.interceptors.response.use(function (resp: AxiosResponse) {
    return resp;
}, function (err: AxiosError) {
    if (err.config && err.config.url?.includes("/user")) {
        localStorage.setItem(Temp.Token,'')
    }

    return Promise.reject(err);
});
